package com.zy.controller;

import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/send")
public class SendController {

    @Autowired
    private StreamBridge streamBridge;

    @GetMapping(value = "/message")
    public String sendMessage(String message) {
        Map<String, Object> map = new HashMap<>();
        map.put(MessageConst.PROPERTY_TAGS, "stefanie-output-topic");
        // 延迟消息发送：第三等级（10s）[1s、5s、10s、30s、1m、2m...1h、2h]
        map.put(MessageConst.PROPERTY_DELAY_TIME_LEVEL, "3");

        MessageHeaders messageHeaders = new MessageHeaders(map);
        Message<String> messageStr = MessageBuilder.createMessage(message, messageHeaders);
        // 消息写入output当中
        streamBridge.send("product-out-0", messageStr);

        return messageStr.toString();

    }
}
